<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.equipmentTypeId"
        placeholder="设备类型"
        @change="selectEquipmentType(listQuery.equipmentTypeId)"
      >
        <el-option
          v-for="item in equipmentTypeList"
          :key="item.id"
          :label="item.typeName"
          :value="item.id"
          :disabled="item.disabled"
        >
        </el-option>
      </el-select>
      <!-- {{ listQuery.equipmentProductId }} -->
      <el-select
        v-model="listQuery.equipmentProductId"
        placeholder="设备名称"
        @change="selectEquipmentProduct(listQuery.equipmentProductId)"
      >
        <el-option
          v-for="item in equipmentProductList"
          :key="item.id"
          :label="item.typeName"
          :value="item.id"
          :disabled="item.disabled"
        >
        </el-option>
      </el-select>

      <!-- {{listQuery.equipmentModelId}} -->
      <el-select
        v-model="listQuery.equipmentModelId"
        placeholder="设备型号"
        @change="selectEquipmentModel(listQuery.equipmentModelId)"
      >
        <el-option
          v-for="item in equipmentModelList"
          :key="item.id"
          :label="item.typeName"
          :value="item.id"
          :disabled="item.disabled"
        >
        </el-option>
      </el-select>

      <DictSelect
        v-model="listQuery.hide"
        placeholder="设备使用情况"
        type="equipment-use-status"
      />

      <el-input
        v-model="listQuery.userName"
        class="filter-item"
        clearable
        placeholder="MAC地址"
        style="width: 150px"
        @keyup.enter.native="handleFilter"
      />

      <el-input
        v-model="listQuery.mobile"
        class="filter-item"
        clearable
        placeholder="用户手机号"
        style="width: 150px"
        @keyup.enter.native="handleFilter"
      />

      <el-button
        v-waves
        v-permission="['sys:role:list']"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >搜索</el-button
      >
      <el-button
        v-waves
        style="margin-right: 153px; float: right"
        type="primary"
        icon="el-icon-plus"
        @click="addEquipment()"
        >录入设备</el-button
      >
    </div>

    <el-table
      :key="tableKey"
      :data="list"
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="设备id" prop="id" width="70px" align="center" />
      <!-- <el-table-column label="注册时间" prop="registeredAt" align="center" /> -->
      <el-table-column label="设备类型" prop="userName" align="center" />
      <el-table-column label="设备名称" prop="mobile" align="center" />
      <el-table-column label="设备型号" prop="mobile" align="center" />
      <el-table-column label="MAC地址" prop="mobile" align="center" />
      <el-table-column label="使用情况" prop="mobile" align="center" />
      <el-table-column label="操作" align="center" width="500px">
        <template slot-scope="{ row }">
          <el-button
            v-waves
            v-permission="['sys:role:save']"
            style="margin-left: 10px"
            type="primary"
            @click="disable(row.id, row.status)"
            >{{ !row.status ? "启用" : "停用" }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNo"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />

    <!-- 以下是录入设备的弹窗 -->
    <el-dialog
      title="设备录入"
      :visible.sync="dialogAddEquipment"
      width="600px"
    >
      <el-form >
        <el-form-item label="设备类型" :label-width="formLabelWidth">
          <el-select
            v-model="equipmentTypeIdDialog"
            placeholder="设备类型"
            @change="selectEquipmentTypeDialog(equipmentTypeIdDialog)"
          >
            <el-option
              v-for="item in equipmentTypeList"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称" :label-width="formLabelWidth">
          <el-select
            v-model="equipmentProductDialog"
            placeholder="设备名称"
            @change="selectEquipmentProductDialog(equipmentProductDialog)"
          >
            <el-option
              v-for="item in equipmentProductListDialog"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" :label-width="formLabelWidth">
          <el-select v-model="equipment.equipmentModelDialog" placeholder="设备型号">
            <!-- @change="selectEquipmentModelDialog(equipment.equipmentModelDialog)" -->

            <el-option
              v-for="item in equipmentModelListDialog"
              :key="item.id"
              :label="item.typeName"
              :value="item.id"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="MAC地址" :label-width="formLabelWidth">
          <el-input
            style="width: 193px"
            v-model="equipment.macAddress"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>

      <div style=" text-align: center;" slot="footer" class="dialog-footer">
        <el-button  type="primary" @click="dialogAddEquipmentSubmit = false"
          >确 定</el-button
        >
        <el-button @click="dialogAddEquipment = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 以上是录入设备的弹窗 -->
  </div>
</template>

<script>
import {
  getList, //获取设备类型的list
  getEquipmentTypeList,
  getProductInfoList,
} from "@/api/equipment/equipment.js";

import DatePicker from "@/components/DatePickerUser";
import waves from "@/directive/waves"; // Waves directive
import DictSelect from "@/components/DictSelect";
import LineChartVue from "../dashboard/admin/components/LineChart.vue";
export default {
  name: "EquipmentTable",
  directives: { waves },
  filters: {},
  components: {
    DictSelect,
  },

  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        orderByField: "createdAt",
        orderBy: "desc",

        hide: "",

        //选择的设备类型id
        equipmentTypeId: "",

        //选择的设备名称id
        equipmentProductId: "",
        equipmentModelId: "",
      },

      //设备类型  集合
      equipmentTypeList: [],
      //设备产品  集合
      equipmentProductList: [],
      //设备型号  集合
      equipmentModelList: [],

      //对话弹窗里面的设备类型list
      // equipmentTypeListDialog :[],

      //录入设备的弹窗
      dialogAddEquipment: false,
      formLabelWidth: "120px",
      equipmentTypeIdDialog: "",

      equipmentProductListDialog: [],
      equipmentProductDialog: "",
      equipmentModelListDialog: [],
      
      //添加的新的对象
      equipment:{
        equipmentModelDialog: "",
        macAddress: "",
      } 

    };
  },
  created() {
    // 加载信息 获取设备类型
    this.getEquipmentTypeList();
  },
  methods: {
    //获取设备类型 ,不分页
    getEquipmentTypeList() {
      getEquipmentTypeList().then((response) => {
        //清空下级的数据
        this.equipmentTypeList = response;
        // this.equipmentTypeListDialog =response  ;
      });
    },

    //选择设备类型
    selectEquipmentType(id) {
      //根据父id 把子级选出来
      getProductInfoList(id).then((response) => {
        //清空下级的数据
        this.equipmentProductList = null;
        this.listQuery.equipmentProductId = "";
        this.equipmentModelList = null;
        this.listQuery.equipmentModelId = "";

        this.equipmentProductList = response;
      });
    },
    //选择设备产品  。。。显示型号
    selectEquipmentProduct(id) {
      //根据父id 把子级选出来
      getProductInfoList(id).then((response) => {
        this.equipmentModelList = null;
        this.listQuery.equipmentModelId = "";

        this.equipmentModelList = response;
      });
    },
    selectEquipmentModel(id) {
      //根据父id 把子级选出来
    },

    //添加设备的按钮
    addEquipment() {
      //打开录入设备的按钮,,,,
      this.dialogAddEquipment = true;
    },

    //选择类型 的对话框 change 事件
    selectEquipmentTypeDialog(id) {
      //2 ,发送ajax 查询后面的数据并且展示
      getProductInfoList(id).then((response) => {
        //1,清空后面的数据
        this.equipmentProductListDialog = null;
        this.equipmentProductDialog = "";

        this.equipmentModelListDialog = null;
        this.equipment.equipmentModelDialog = "";

        this.equipmentProductListDialog = response;
      });
    },

    //选择产品 的对话框 change事件
    selectEquipmentProductDialog(id) {
      //1 清空后面的数据
      this.equipmentModelListDialog = null;
      this.equipment.equipmentModelDialog = "";

      //2 发送ajax 查询后面的数据并且展示
      getProductInfoList(id).then((response) => {
        this.equipmentModelListDialog = response;
      });
    },
     
     //添加新数据的窗口
    dialogAddEquipmentSubmit(){
     //发送ajax 添加数据
 
    },

    // 获取数据
    getList() {
      if (this.$refs["cascader"] != undefined) {
        var nodes = this.$refs["cascader"].getCheckedNodes();
        for (var i = 0; i < nodes.length; i++) {
          this.listQuery.communityIdArr = Array.from(
            new Set(this.listQuery.communityIdArr.concat(nodes[i].path))
          );
        }
      }
      //    console.log(this.listQuery);
      getList(this.listQuery).then((response) => {
        this.list = response.list;
        this.total = response.totalCount;
      });
    },

    handleFilter() {
      this.listQuery.pageNo = 1;
      this.getList();
    },
  },
};
</script>
